/*
 * @Author: your name
 * @Date: 2021-04-29 18:35:29
 * @LastEditTime: 2021-04-29 19:34:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \offer20k\项目代码\management\src\models\pageModel.ts
 */
//页面管理
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import * as http from '@/services/index';
import { IPageItem } from '@/utils/pageManagement';
import { message } from 'antd';
export interface pageModelState {
  PagePagingList: IPageItem[];
}

export interface pageModelType {
  namespace: 'page';
  state: pageModelState;
  effects: {
    getPagePagingList: Effect;
  };
  reducers: {
    save: Reducer<pageModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<pageModelState>;
  };
  subscriptions: { setup: Subscription };
}

const pageModel: pageModelType = {
  namespace: 'page',

  state: {
    PagePagingList: [],
  },

  effects: {
    *getPagePagingList({ payload }, { call, put }) {
      let result = yield call(http.getList);
      if (result.statusCode === 200) {
        message.success('请求成功');
        yield put({
          type: 'save',
          payload: {
            PagePagingList: result.data[0],
          },
        });
      } else {
        message.error('请求数据失败，请稍后重试');
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          });
        }
      });
    },
  },
};

export default pageModel;
